/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jfMain.java
 *
 * Created on Feb 7, 2012, 12:15:43 PM
 */
package kmean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import untils.ExampleFileFilter;
import untils.PhanLop;

/**
 *
 * @author Kanet
 */
public class jfMain extends javax.swing.JFrame {

    /**
     * Creates new form jfMain
     */
    public jfMain() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnBrowser = new javax.swing.JButton();
        tfSoPhanLop = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnBatDau = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtTapTinDoc = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtThongKe = new javax.swing.JTextArea();
        btnGhiTapTin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("K-mean clustering");

        btnBrowser.setText("Browser");
        btnBrowser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowserActionPerformed(evt);
            }
        });

        tfSoPhanLop.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tfSoPhanLop.setEnabled(false);

        jLabel1.setText("Số K:");

        btnBatDau.setText("Phân lớp");
        btnBatDau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBatDauActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel2.setText("Phân lớp K-means");

        txtTapTinDoc.setEditable(false);
        txtTapTinDoc.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        txtTapTinDoc.setEnabled(false);

        txtThongKe.setColumns(20);
        txtThongKe.setRows(5);
        jScrollPane1.setViewportView(txtThongKe);

        btnGhiTapTin.setText("Ghi Tập Tin");
        btnGhiTapTin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGhiTapTinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfSoPhanLop, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnBatDau)))
                .addContainerGap(314, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 113, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addGap(113, 113, 113))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(txtTapTinDoc, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBrowser)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGhiTapTin)
                        .addContainerGap(336, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTapTinDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBrowser))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfSoPhanLop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBatDau)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnGhiTapTin)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    File tapTin = null;
    PhanLop phanLop;
    private void btnBrowserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowserActionPerformed
        try {
            // TODO add your handling code here:
            JFileChooser jchooser = new JFileChooser();
            //Tạo định dạng file
            //Thêm định dạng file test
            ExampleFileFilter ff = new ExampleFileFilter("txt", "Text File");
            jchooser.addChoosableFileFilter(ff);
            //Thêm định dạng file csv
            ff = new ExampleFileFilter("csv", "Comma delimited");
            jchooser.addChoosableFileFilter(ff);
            
            jchooser.showOpenDialog(this);
            
            tapTin = jchooser.getSelectedFile();
            this.txtTapTinDoc.setText(tapTin.getPath());
            phanLop = new PhanLop();
            phanLop.DocTapTin(tapTin.getPath().toString());
            this.tfSoPhanLop.enable(true);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(jfMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(jfMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnBrowserActionPerformed
    
    private void btnBatDauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBatDauActionPerformed
        // TODO add your handling code here:
        Date d = new Date();
        long t1 = d.getTime();
        phanLop.setK(Integer.parseInt(this.tfSoPhanLop.getText()));
        phanLop.TienHanhPhanLop(phanLop.getK());
        d = new Date();
        long t2 = d.getTime();
        double t = (double) (t2 - t1) / 1000;
        String tocDoThuatToan = String.format("Tốc độ thuật toán = %.3f s", t);
        this.txtThongKe.setText(tocDoThuatToan);

        //Tính tỷ lệ phân lớp cho từng nhóm
        String s = phanLop.TinhTyLePhanNhom();
        this.txtThongKe.setText(this.txtThongKe.getText() + "\n" + s);
        
        
        
        
    }//GEN-LAST:event_btnBatDauActionPerformed
    
    private void btnGhiTapTinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGhiTapTinActionPerformed
        try {
            // TODO add your handling code here:
            JFileChooser jchooser = new JFileChooser();

            //Thêm định dạng file
            //Thêm định dạng file text
            ExampleFileFilter ff = new ExampleFileFilter("txt", "Text File");
            jchooser.addChoosableFileFilter(ff);
            //Thêm định dạng file csv
            ff = new ExampleFileFilter("csv", "Comma delimited");
            jchooser.addChoosableFileFilter(ff);
            
            jchooser.showSaveDialog(this);
            
            tapTin = jchooser.getSelectedFile();
            
            ff = (ExampleFileFilter) jchooser.getFileFilter();
            
            int s = ff.getDescription().indexOf(".");
            
            String str = ff.getDescription().substring(s, s + 4);
            tapTin = new File(tapTin.getPath() + str);
            
            
            
            phanLop.GhiTapTin(tapTin);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }//GEN-LAST:event_btnGhiTapTinActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                new jfMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBatDau;
    private javax.swing.JButton btnBrowser;
    private javax.swing.JButton btnGhiTapTin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tfSoPhanLop;
    private javax.swing.JTextField txtTapTinDoc;
    private javax.swing.JTextArea txtThongKe;
    // End of variables declaration//GEN-END:variables
}
